package it.unical.canonaco.rende.igpe.game.cbv.guilayer.gamemanagermenu.storemenu.itemshop;

import it.unical.canonaco.rende.igpe.game.cbv.guilayer.components.ImageCache;
import it.unical.canonaco.rende.igpe.game.cbv.guilayer.components.ImageProvider;
import it.unical.canonaco.rende.igpe.game.cbv.guilayer.components.OverallGUILayoutFactory;
import it.unical.canonaco.rende.igpe.game.cbv.guilayer.gamemanagermenu.storemenu.CharInfoPanel;
import it.unical.canonaco.rende.igpe.game.cbv.guilayer.gamemanagermenu.storemenu.StorePanel;
import it.unical.canonaco.rende.igpe.game.cbv.guilayer.guievents.BagClickEvent;
import it.unical.canonaco.rende.igpe.game.cbv.guilayer.guievents.CharacterClickEvent;
import it.unical.canonaco.rende.igpe.game.cbv.guilayer.guievents.GraphicEvent;
import it.unical.canonaco.rende.igpe.game.cbv.guilayer.guievents.GraphicEventListener;
import it.unical.canonaco.rende.igpe.game.cbv.guilayer.guievents.GraphicEventManager;
import it.unical.canonaco.rende.igpe.game.cbv.guilayer.guievents.ItemBoughtEvent;
import it.unical.canonaco.rende.igpe.game.cbv.guilayer.guievents.ItemSoldEvent;
import it.unical.canonaco.rende.igpe.game.cbv.guilayer.guievents.ShopItemClickEvent;
import it.unical.canonaco.rende.igpe.game.cbv.logiclayer.engine.component.pawn.Character;
import it.unical.canonaco.rende.igpe.game.cbv.logiclayer.engine.component.pawn.items.Item;
import it.unical.canonaco.rende.igpe.game.cbv.logiclayer.engine.component.pawn.items.ItemFactory;
import it.unical.canonaco.rende.igpe.game.cbv.logiclayer.engine.component.pawn.stats.Role;
import it.unical.canonaco.rende.igpe.game.cbv.logiclayer.engine.component.pawn.stats.Stats;
import it.unical.canonaco.rende.igpe.game.cbv.logiclayer.engine.managers.StoreManager;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontFormatException;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.Insets;
import java.awt.Paint;
import java.awt.PaintContext;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.geom.AffineTransform;
import java.awt.geom.Rectangle2D;
import java.awt.geom.RoundRectangle2D;
import java.awt.image.ColorModel;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;

import javax.imageio.ImageIO;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
import javax.swing.BorderFactory;
import javax.swing.DefaultListModel;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;
import javax.swing.UIManager;
import javax.swing.border.EtchedBorder;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.plaf.metal.MetalScrollBarUI;

class CharItemInfoPanel extends CharInfoPanel implements GraphicEventListener {

	private JLabel[] itemStatsLabels = new JLabel[Stats.TOTAL_STATISTICS];
	private JLabel itemIcon = new JLabel();
	 
	CharItemInfoPanel(final GraphicEventManager gem, final StoreManager storeManager, HashMap<String,String> settings,
			 ImageCache imgCache) {
		super(gem,storeManager,settings,imgCache);
		this.gem.addListener(this, ShopItemClickEvent.class);
		this.gem.addListener(this, ItemBoughtEvent.class);
		this.gem.addListener(this, ItemSoldEvent.class);

		gbc.gridx = 2;
		gbc.gridy = 0;
//		gbc.anchor = GridBagConstraints.REMAINDER;
//		gbc.ipady = Integer.parseInt(settings.get(StorePanel.AVATAR_IMAGE_HEIGHT));
		gbc.anchor = GridBagConstraints.PAGE_END;
		this.add(itemIcon,gbc);
		
//		gbc.insets = new Insets(Integer.parseInt(settings.get(StorePanel.AVATAR_IMAGE_HEIGHT)),0,0,0);
//		gbc.ipady = Integer.parseInt(settings.get(StorePanel.AVATAR_IMAGE_HEIGHT));
		for(int i = 0; i < Stats.TOTAL_STATISTICS;i++){
			gbc.gridy = i+1;
			gbc.anchor = GridBagConstraints.FIRST_LINE_START;
			itemStatsLabels[i] = new JLabel();
			itemStatsLabels[i].setOpaque(true);
			itemStatsLabels[i].setBackground(new Color(255,255,255,200));
			itemStatsLabels[i].setPreferredSize(new Dimension(Integer.parseInt(settings.get(StorePanel.STATS_VALUE_WIDTH)), 
												Integer.parseInt(settings.get(StorePanel.STATS_JLABEL_HEIGHT))));
			this.add(itemStatsLabels[i], gbc);
		}
	
		final InventoryPanel bag = new InventoryPanel(storeManager,settings,imgCache);
		gbc.gridx = 0;
		gbc.gridy = 8;
		gbc.gridwidth = GridBagConstraints.REMAINDER;
		gbc.anchor = GridBagConstraints.PAGE_START;
		this.add(bag,gbc);
	}


	private void clearItemLabels() {
		for(int i = 0; i<Stats.TOTAL_STATISTICS; i++)
			itemStatsLabels[i].setText(StorePanel.EMPTY);
		itemIcon.setIcon(null);
		itemIcon.setBorder(null);
	}
	
	private void printItemStats(Item item){
		itemIcon.setIcon(new ImageIcon(imgCache.getImage(settings.get(StorePanel.ITEM_IMAGE_PATH)+item.getName()+settings.get(StorePanel.ITEM_IMAGE_EXTENSION))));
		itemIcon.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createLineBorder(Color.DARK_GRAY.brighter(),2), BorderFactory.createLineBorder(Color.LIGHT_GRAY,2)));
		String fontColor;
		for(int i = 0; i<Stats.TOTAL_STATISTICS; i++) {
			int stat = (int) item.getStats(i);
			String signedStat = stat >= 0 ? stat == 0 ? StorePanel.EMPTY : StorePanel.PLUS+stat+StorePanel.SPACE : StorePanel.EMPTY+stat+StorePanel.SPACE;
			itemStatsLabels[i].setText(signedStat);
			fontColor = (stat >= 0) ? (stat == 0) ? settings.get(StorePanel.COLOR_NEUTRAL) :
					settings.get(StorePanel.COLOR_POSITIVE) : settings.get(StorePanel.COLOR_NEGATIVE);
			itemStatsLabels[i].setFont(guiCreator.getHeaderFont().deriveFont(Float.parseFloat( settings.get(StorePanel.STATS_VALUE_TEXT_SIZE))));
			itemStatsLabels[i].setForeground(Color.decode(StorePanel.HASH+fontColor));
			itemStatsLabels[i].setBackground(new Color(255,255,255,200));
		}
	}
	
	@Override
	public void notify(GraphicEvent event) {
		
		super.notify(event);
		
		if(event.getClass() == ShopItemClickEvent.class){
			Item item = ((ShopItemClickEvent) event).getItem();
		 if(item == null) clearItemLabels();
		 else printItemStats(item);
		 repaint();
		} else if( event.getClass() == ItemBoughtEvent.class || event.getClass() == ItemSoldEvent.class){
			
			printCharStats(storeManager.getCurrentSelectedChar().getStats());
			repaint();
		}
	}

}
	

	
